<?php
namespace Index\Controller;
use Think\Controller;
class TeacherController extends Controller {

    public function indexAction(){
    	$uid = I('session.uid');
        $id = I('get.id');
        $this->assign('teacherid',$id );
        $Model = M();
          //用户信息
        $sql = "select face,username from user where id ='{$uid}'";
        $userInfo = $Model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);

        //全部课程分类
        $sql = "select * from category";
        $category = $Model->query($sql);
        $categoryInfo= $this->_fibTree($category,0);
        $this->assign('categoryInfo',$categoryInfo);

        $sql = "select teachername from teacher where id = '{$id}'";
        $teacherInfo = $Model->query($sql);

        $sql = " select * from (select *,sum(t4.apply),ifnull(sum(t4.apply),0) as applynum,count(t4.courseid),ifnull(count(t4.courseid),0) as coursenum,sum(t4.favorite),ifnull(sum(t4.favorite),0) as favoritenum from (select t1.courseid,t1.id,t1.teachername,t1.teacherface,t1.brief,t1.skill,t2.apply,t3.favorite from teacher as t1 left join (select courseid,count(*) as apply from application group by courseid) as t2 on t1.courseid = t2.courseid left join (select courseid,count(*) as favorite from course_favorite group by courseid) as t3 on t1.courseid = t3.courseid) as t4 group by t4.teachername) as t5 where t5.teachername ='{$teacherInfo[0]['teachername']}'";
        $teacherInfo = $Model->query($sql);
        $this->assign('teacherInfo',$teacherInfo);

        $sql = "select t2.id,t2.coursename,t2.price,t2.introduction,t2.picture,t1.teachername,t1.brief,t1.teacherface,t1.skill,t3.apply,ifnull(t3.apply,0) as applynum from teacher t1 left join course t2 on t2.id = t1.courseid left join (select courseid,count(*) as apply from application group by courseid) as t3 on t1.courseid = t3.courseid where t1.teachername ='{$teacherInfo[0]['teachername']}'";
        $courseInfo = $Model->query($sql);
        $this->assign('courseInfo',$courseInfo);

        $count = count($teacherInfo);
        $Page = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(6)
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);

    	$this->display();
    }

    //初始判断是否关注的ajax
    public function areAttentionAction(){
        if(!IS_AJAX){
            $data = "非法的请求方式";
            $this->ajaxReturn($data);
        }
        $uid = I('session.uid');
        $id = I('get.id');
        $Model = M();
        $sql = "select * from attention where userid = '{$uid}' and teacherid = '{$id}'";
        $attention = $Model->query($sql);
        if(empty($attention)){
            $data = array(
                'success' => 0,
                'messageInfo'=>'未关注'
            );  
        }else{
            $data = array(
                'success' => 1,
                'messageInfo'=>'已关注'
            );
        }
        $this->ajaxReturn($data);

    }

     //加入关注的ajax
    public function addAttentionAction(){
        if(!IS_AJAX){
            $data = "非法的请求方式";
            $this->ajaxReturn($data);
        }
        $uid = I('session.uid');
        if(empty($uid)){
            $data = array(
                'success' => 0,
                'messageInfo'=>'用户未登录'
            );
            $this->ajaxReturn($data);
        }
        $id = I('get.id');
        $Model = M();
        $sql = "select * from attention where userid = '{$uid}' and teacherid = '{$id}'";
        $attention = $Model->query($sql);
        if(empty($attention)){
            $data = array(
                'success' => 1,
                'messageInfo'=>'未收藏'
            );
            $time = time();
            $sql = "insert into attention(teacherid,userid,createtime) values('{$id}','{$uid}','{$time}')";
            $Model->execute($sql);
        }else{
            $data = array(
                'success' => 0,
                'messageInfo'=>'已收藏'
            );
        }
        $this->ajaxReturn($data);

    }


    //取消关注的ajax
    public function delAttentionAction(){
        if(!IS_AJAX){
            $data = "非法的请求方式";
            $this->ajaxReturn($data);
        }
        $uid = I('session.uid'); 
        $id = I('get.id');
        $Model = M();
        $sql = "delete from attention where userid = '{$uid}' and teacherid = '{$id}' ";
        $Model->execute($sql);
        $data = array(
                'success' => 1,
                'messageInfo'=>'已删除'
            );
        $this->ajaxReturn($data);

    }

     //全部课程分类调用方法
    private function _fibTree($arr, $fid = 0) {
        $tree = array();
        $temp = array();
        if($arr) {
            foreach($arr as $k=>$v) {
                if($v['fid'] == $fid) {
                    $temp = $this->_fibTree($arr, $v['id']);
                    $temp AND $v['son'] = $temp;
                    $tree[] = $v;
                }
            }
        }
        return $tree;
    }     


}